﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using Telerik.Web.UI;
using Acms.Bll;

namespace Acms.AcmsControls.ContentControls.Admin
{
    public class PdfAdminEdit : BaseAdminEdit<PdfContentControlProperties>
    {
        protected override string GetCssClass
        {
            get { return "pdf"; }
        }

        public override void PropertiesLoaded(PdfContentControlProperties properties)
        {
            if (properties != null)
            {
                name.Text = properties.Name;
                lnk.NavigateUrl = RenderFile.GetUrl(properties.ResourceId, Page);
            }
        }

        RadUpload upload = new RadUpload();
        TextBox name = new TextBox();
        HyperLink lnk = new HyperLink();

        protected override void CreateChildControls()
        {
            upload.ControlObjectsVisibility = ControlObjectsVisibility.None;
            this.Controls.Add(upload);

            this.Controls.Add(name);
            lnk.Target = "_Blank";
            lnk.Text = "Click to view PDF";
            this.Controls.Add(lnk);
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            upload.RenderControl(writer);

            writer.AddAttribute(HtmlTextWriterAttribute.Class, "form");
            writer.RenderBeginTag(HtmlTextWriterTag.Ul);

            writer.RenderBeginTag(HtmlTextWriterTag.Li);

            writer.AddAttribute(HtmlTextWriterAttribute.For, string.Concat(this.ClientID, name.ClientID));
            writer.RenderBeginTag(HtmlTextWriterTag.Label);
            writer.Write("Name:");
            writer.RenderEndTag();//label

            name.RenderControl(writer);

            writer.RenderEndTag();//li

            writer.RenderEndTag();//ul

            if (!string.IsNullOrEmpty(lnk.NavigateUrl))
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Class, "form");
                writer.RenderBeginTag(HtmlTextWriterTag.Ul);

                writer.RenderBeginTag(HtmlTextWriterTag.Li);

                writer.AddAttribute(HtmlTextWriterAttribute.For, string.Concat(this.ClientID, lnk.ClientID));
                writer.RenderBeginTag(HtmlTextWriterTag.Label);
                writer.Write("Existing PDF:");
                writer.RenderEndTag();//lable

                lnk.RenderControl(writer);

                writer.RenderEndTag();//li

                writer.RenderEndTag();//ul
            }
        }

        protected override void SaveToProperties(PdfContentControlProperties properties, Acms.Bll.PostControl control)
        {
            if (upload.UploadedFiles.Count == 1)
            {
                File file = File.CreateInstance(File.FileTypes.Pdf, upload.UploadedFiles[0].GetNameWithoutExtension(), upload.UploadedFiles[0].ContentType, upload.UploadedFiles[0].ContentLength, upload.UploadedFiles[0].InputStream);

                file.BeginTransaction();
                control.UseTransaction(file.TransactionKey);

                file.Update();

                properties.ResourceId = file.Id;
            }
            properties.Name = name.Text.Trim();
        }
    }
}
